package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.object.pojo.BatteryModelDetail;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.BatteryModelDetailService;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 电池方案详细
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Validated
@RestController
@RequestMapping(value = "/batteryModelDetail", produces = BaseController.MEDIA_JSON_UTF8)
public class BatteryModelDetailController extends BaseController {
    @Autowired
    private BatteryModelDetailService batteryModelDetailService;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 添加
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.BATTERYMODELDETAIL, methods = MethodEnum.BATTERYMODELDETAIL_ADD)
    public JsonResult add(BatteryModelDetail batteryModelDetail, HttpServletRequest request) {
        /* 校验 */
        Assert.notNull(batteryModelDetail.getBatteryModelId(), "电池方案id不能为空");
        Assert.notNull(batteryModelDetail.getMinVol(), "最低电压不能为空");
        Assert.notNull(batteryModelDetail.getMaxVol(), "最高电压不能为空");
        Assert.notNull(batteryModelDetail.getBatteryPer(), "电量百分比不能为空");
        Assert.notNull(batteryModelDetail.getMilage(), "剩余续航里程不能为空");
        /* 校验 */
//		List<BatteryModelDetail> batteryModelDetails = batteryModelDetailService.getByBatteryModelId(
//				batteryModelDetail.getBatteryModelId());
//		if (batteryModelDetails.size() > 12) {
//			throw new BaseException("只能定义12个");
//		}
        batteryModelDetailService.insert(batteryModelDetail);

        return JsonResult.ok(null);
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.BATTERYMODELDETAIL, methods = MethodEnum.BATTERYMODELDETAIL_DEL)
    public JsonResult del(Integer batteryModelDetailId, HttpServletRequest request) {

        Assert.notNull(batteryModelDetailId, "batteryModelDetailId不能为空");
        batteryModelDetailService.del(batteryModelDetailId);

        return JsonResult.ok(null);
    }

    /**
     * 根据电池方案id查询
     */
    @RequestMapping("/getByBatteryModelId")
    public JsonResult getByBatteryModelId(Integer batteryModelId,
                                          @RequestParam(defaultValue = "500") Integer rowCount,
                                          @RequestParam(defaultValue = "1") Integer pageNO,
                                          HttpServletRequest request) {
        /* 校验 */
        Assert.notNull(batteryModelId, "batteryModelId不能为空");
        Integer total = batteryModelDetailService.getTotal(batteryModelId);
        List<BatteryModelDetail> batteryModelDetails = batteryModelDetailService.getByBatteryModelId(batteryModelId, rowCount, pageNO);

        return JsonResult.ok(batteryModelDetails, total);
    }

    /**
     * 批量添加
     */
    @RequestMapping("/editList")
    @OperateLog(module = ModuleEnum.BATTERYMODELDETAIL, methods = MethodEnum.BATTERYMODELDETAIL_ADD)
    public JsonResult addList(String remark,
                              @NotNull(message = "ver.name.notNull") String name,
                              @NotNull(message = "ver.maxVols.notNull") String maxVols,
                              @NotNull(message = "ver.minVols.notNull") String minVols,
                              @NotNull(message = "ver.batteryPers.notNull") String batteryPers,
                              @NotNull(message = "ver.milages.notNull") String milages,
                              @NotNull(message = "ver.batteryModelId.notNull") Integer batteryModelId,
                              String pwd, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (remark == null) {
            remark = "";
        }

        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginInfo, OperaPswEnum.OPERA_PSW_OPERATION_CONFIG.getModule(), pwd);

        batteryModelDetailService.editList(loginInfo, batteryModelId, name, remark, maxVols, minVols, batteryPers, milages);
        return JsonResult.ok();
    }

}